package com.example.mymarsphotos.ui

import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.mymarsphotos.R
import com.example.mymarsphotos.ui.screen.HomeScreen
import com.example.mymarsphotos.ui.screen.MarsViewModel

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun MarsPhotosApp() {
    val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
    Scaffold(
        modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
        topBar = { MarsTopAppBar(scrollBehavior) }
    ) { intentPadding ->
        val marsViewModel: MarsViewModel = viewModel(factory = MarsViewModel.Factory)
        HomeScreen(
            marsUiState = marsViewModel.marsUiState,
            retryAction = marsViewModel::getMarsPhotos,
            contentPadding = intentPadding
        )
    }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MarsTopAppBar(scrollBehavior: TopAppBarScrollBehavior) {
    CenterAlignedTopAppBar(
        scrollBehavior = scrollBehavior,
        title = {
            Text(
                text = stringResource(R.string.app_name),
                style = MaterialTheme.typography.headlineSmall
            )
        }
    )
}

@Preview
@Composable
fun MarsPhotosAppPreview() {
    MarsPhotosApp()
}